package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class DelOperation implements IOperation {
    @Override
    public void work(BookList bookList) {
        System.out.println("删除图书....");
        System.out.println("请输入想要删除的书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();
        //查找是否有这个书
        int size = bookList.getSize();
        for (int i = 0; i < size; i++) {
            Book book = bookList.getBook(i);
            if (name.equals(book.getName())) {
                //找到了这本书,进行删除操作
                for(int j = i;j<size-1;j++){
                    bookList.setBook(j,bookList.getBook(j+1));
                }
                bookList.setSize(size-1);
                //最后一个置为null
                bookList.setBook(size-1,null);
                System.out.println("删除成功...");
                return;
            }
        }
        //没找到这本书
        System.out.println("很抱歉，没有找到这本书...");
    }
}
